



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;



/*@uses: Used to find No. of files modified by  a bug was found in our database
 * [It was found that even though I ma using the same version it some time happen that I am not able to find the file modified
 * because 1. There are many version reported with a bug and I am using first available versions].
 * * */

public class a_file_modified 
{
   public Connection conn =  null;
   private String dbname = "2apache";
   private String passwd= "123";
   private String userid = "root"; 
   ArrayList<String>  bugids = null;	
   public String selected_bug_table = "a_selected_bugs";//<<<<<<please set
  
	
public void process(Connection conn)
 {
	   bugids =  new ArrayList<String>();
	   bugids =  get_bugids(conn,bugids);
	   System.out.println("No Of bugids = "+bugids.size());
	      
	   int No_of_bugids = 0; 
	  
	   for(int i =0;i<bugids.size();i++)
	   {
		   String bugid = bugids.get(i);
		   System.out.println("NO EXCEPTION bugid="+bugid);
		   
		   
		   String bug_version_table  =  get_table2(conn,bugid);		 
		   System.out.println("Bugid = "+bugid +" table="+bug_version_table);
		   ArrayList<String> bug_valid_svn_file = get_bug_svn_valid_files(conn,bugid,bug_version_table);
		   
		   if(bug_valid_svn_file.size()!=0)
		   {
			  int mod_file_found = bug_valid_svn_file.size(); 
		      insert_to_mod_table(conn,bugid,mod_file_found);
		   } 
		}//for
 }


private void insert_to_mod_table(Connection conn2, String bugid,int no_of_mod_file_found) 
{

	Statement stmt =  null;
	try
	{
		stmt =  conn.createStatement();
		String str = "insert into a_file_mod_found values ('"+bugid+"',"+no_of_mod_file_found+")";
		stmt.executeUpdate(str);
		
		if(stmt!=null)
			stmt.close();
	
	} catch (SQLException e) 
	{ 	e.printStackTrace(); }
	
}//insert



// Method to retrive valid svn files modified for a bug, becuase it might happen that some files
// modified for a aparticular bug may not be present in the affected version.
private ArrayList<String> get_bug_svn_valid_files(Connection conn, String bugid, String table) 
{
	ArrayList<String> valid_svn_files = new ArrayList<String>(); 
	ArrayList<String> table_files = get_table_files(table,conn);
	String bug_svn_q ="select distinct bugid, file_name from  a_bug_svn_m_file  where bugid='"+bugid+"'";
	//System.out.println("Bug_svn_q="+bug_svn_q);
	   
	   Statement stmt2;
	   try 
	   {
		stmt2 = conn.createStatement();
		stmt2.execute(bug_svn_q);
		ResultSet rs2 =  stmt2.getResultSet();
		while(rs2.next())
		{
			String file_name = rs2.getString("file_name");			
			boolean valid = is_valid_table_file(table_files,file_name);
			
			if(valid)
				valid_svn_files.add(file_name);
			
		}
		
		if(rs2!=  null)
			rs2.close();
		if(stmt2!=null)
			stmt2.close();
		   
	   } catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	return valid_svn_files;
}

private boolean is_valid_table_file(ArrayList<String> table_files,String file_name)
{
   boolean valid  = false;	
   
   for(int i=0;i<table_files.size();i++)
   {
	   String table_file  =  table_files.get(i);
	   if(file_name.equalsIgnoreCase(table_file))
	   {
		  // System.out.println("file name = "+file_name);
		  // System.out.println("table_file="+table_file);
		   valid =  true;
		   break;
	   }
   }
   
   return valid;
}

//to check that files in modified by a bug actually exists in the table we are considering
private ArrayList<String>  get_table_files(String version_table, Connection conn)
{
	
	ArrayList<String> table_files =  new ArrayList<String>();
	
	String table_file_q  =  "select distinct file_name from "+version_table;
	//System.out.println("Table q = "+table_file_q);
	Statement stmt  =  null;
	try
	{
		stmt=conn.createStatement();
		stmt.execute(table_file_q);
		ResultSet rs =  stmt.getResultSet();
		while(rs.next())
		{
			String file_name =  rs.getString("file_name");
			//file_name =  file_name.replace(".java", "").toLowerCase();			
			table_files.add(file_name); 
		}
		
		if(rs!=null)
			rs.close();
		
		if(stmt!=null)
			stmt.close();
		
	} catch (SQLException e) 
	{e.printStackTrace(); }
	
	
	return table_files;
}

public String get_table2 (Connection conn, String bugid)
{
	
	String v_q = "select version from a_bug_version where bugid='"+bugid+"'";
	 Statement stmt =  null;
     ResultSet rs = null; 
     a_h2_opt ah2 =  new a_h2_opt();
	 String version="";
     
	 try 
	  {
		 stmt=conn.createStatement();
		 stmt.execute(v_q);
		 rs = stmt.getResultSet();
		
		
		 while(rs.next())
		 {
			 version = rs.getString("version");
			System.out.println("Vesrion="+version);
			 
			 boolean valid =ah2.isValidVesion(version,conn); 
			 if(valid)
			 {
				 break;
			 }
		 }//wh
	 
		 if(rs!=null)
				rs.close();
			 
		     if(stmt!=null)
			   stmt.close(); 
		 
	  }catch(SQLException e)
	  {
		  e.printStackTrace();
	  }
	  System.out.println("after valid=");
	  
	 version = formated_version(version);
	  
	 System.out.println("version="+version+ "  formated(table)="+version);
	 //intr();
	  
	  return version;
}


public String formated_version(String version)
{
	 String formated_version="";
	 String v_q = "select map from a_version_map where version='"+version+"'";
	 
	 Statement stmt =  null;
	 
	 try 
	 {
		stmt = conn.createStatement();
		stmt.execute(v_q);
		ResultSet rs  = stmt.getResultSet();
		while(rs.next())
		{
			formated_version = rs.getString("map");
		}
		
	
	 if(rs!=null)
		 rs.close();
	 if(stmt!=null)
		 stmt.close();
	 } catch (SQLException e) 
	 {	e.printStackTrace(); }
	 
	 return formated_version;	
}

private ArrayList<String> get_bugids(Connection conn, ArrayList<String> bugids) 
{

	System.out.println("Hey");
	  String bug_q = "select distinct bugid from "+selected_bug_table;
	  Statement stmt = null;	
	  try 
	  {
		stmt  = conn.createStatement();
		stmt.execute(bug_q);
		ResultSet rs =  stmt.getResultSet();
		while(rs.next())
		{
			String bugid = rs.getString("bugid");
			bugids.add(bugid);
		}
		
		
		if(rs!=null)
			rs.close();		
		if(stmt!=null)
			stmt.close();
		
	  } catch (SQLException e) 
	  { e.printStackTrace();}
	  
	  System.out.println("Here exception");
	  return bugids;
}

public void intr()
{
	 BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
	 System.out.println("INTR");
	 try {
		br.readLine();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 
}


public static void main(String args[])
{
		a_file_modified afm = new a_file_modified();
		db_conn db  =  new db_conn();
		afm.conn  = db.connect(afm.dbname, afm.userid, afm.passwd);
		afm.process(afm.conn);
	    db.close(afm.conn);
}


 
}//class

